import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    //存放用户的默认头像
    avatar_url:
      "https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png",
    api_url: "http://10.1.188.126:8888",

    IsLogin: localStorage.getItem("IsLogin"),
    uid: localStorage.getItem("uid"),
    token: localStorage.getItem("token"),
    phone: localStorage.getItem("phone"),
    nickname: localStorage.getItem("nickname"),
    avatar: localStorage.getItem("avatar"),
  },

  mutations: {
    // 登录
    updateIsLogin(state, payload) {
      state.IsLogin = payload.IsLogin;

      localStorage.setItem("IsLogin", payload.IsLogin);
      localStorage.setItem("token", payload.token);
      localStorage.setItem("phone", payload.phone);
      localStorage.setItem("nickname", payload.nickname);
      localStorage.setItem("avatar", payload.avatar);
      localStorage.setItem("uid", payload.uid);
    },

    // 退出登录
    LoginOut(state) {
      state.IsLogin = undefined;
      localStorage.removeItem("IsLogin");
      localStorage.removeItem("uid");
      localStorage.removeItem("token");
      localStorage.removeItem("phone");
      localStorage.removeItem("nickname");
      localStorage.removeItem("avatar");
    },

    // 更新用户信息数据
    updateUserData(state, payload) {
      localStorage.setItem("nickname", payload.nickname);
      localStorage.setItem("avatar", payload.avatar);
    },
  },
  actions: {
  },
  modules: {
  }
})
